package com.neuedu.autosale.sys.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neuedu.autosale.entity.Pager;
import com.neuedu.autosale.entity.Result;
import com.neuedu.autosale.sys.entity.Provider;
import com.neuedu.autosale.sys.service.ProviderService;

@Controller
public class ProviderController {
	
	@Autowired
	private ProviderService providerService;
	
	@RequestMapping("provider/index")
	public String providerIndex() {
		return "provider/providerlist";
	}
	
	@RequestMapping("provider/findall")
	@ResponseBody
	public List<Provider> providerFindall(){
		return providerService.findAll();
	}
	
	@RequestMapping("provider/list")
	@ResponseBody
	public Pager<Provider> providerlist(@RequestParam(value = "pager",defaultValue = "1") Integer page,
			@RequestParam(value = "rows",defaultValue = "10") Integer rows){
		Pager<Provider> pager = new Pager<>(page,rows);
		List<Provider> providerlist = providerService.findListByPager(pager);
		int total = providerService.findTotalByPager(pager);
		pager.setRows(providerlist);
		pager.setTotal(total);
		return pager;
	}
	
	@RequestMapping("provider/add")
	@ResponseBody
	public Result doAdd(Provider provider) {
		Result result = providerService.add(provider);
		return result;
	}
	
	@RequestMapping("provider/edit")
	@ResponseBody
	public Result doEdit(Provider provider) {
		Result result = providerService.edit(provider);
		return result;
	}
	
	@RequestMapping("provider/delete")
	@ResponseBody
	public Result doDelete(Integer providerId) {
		List<Provider> list = new ArrayList<Provider>();
		list.add(new Provider(providerId));
		Result result = providerService.deleteByIds(list);
		return result;
	}
}
